﻿using System;
using System.Collections.Generic;
using System.Linq;
using ComicQ.Domain.Table;
using NHibernate.Linq;

namespace ComicQ.Controller
{
    public class OrderController
    {
        public bool IsValidDate(DateTime rentDate, DateTime returnDate)
        {
            return rentDate < returnDate && 
                rentDate >= DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0));
        }

        public IList<Order> GetAllOrder()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                return session.Query<Order>().ToList();
            }
        }

        public IList<OrderData> GetAllOrderData()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                var orders = session.Query<Order>().ToList();
                return orders.Select(order => new OrderData(order.Id, order.Member.Username, 
                    order.Staff.Username, order.RentDate.ToString(), order.ReturnDate.ToString(), 
                    order.Warranty.Type, order.WarrantyNumber, order.Total.ToString(), 
                    order.OrderStatus.Status)).ToList();
            }
        }

        public void AddOrder(Order order)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Save(order);
                    transaction.Commit();
                }
            }
        }

        public void EditOrder(Order newOrder)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.SaveOrUpdate(newOrder);
                    transaction.Commit();
                }
            }
        }

        public void DeleteOrder(Order order)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Delete(order);
                    transaction.Commit();
                }
            }
        }
    }

    public class OrderData
    {
        public OrderData(Guid id, string member, string staff, string rentDate, string returnDate, string warranty, string warrantyNumber, string total, string status)
        {
            Id = id;
            Member = member;
            Staff = staff;
            RentDate = rentDate;
            ReturnDate = returnDate;
            Warranty = warranty;
            WarrantyNumber = warrantyNumber;
            Total = total;
            OrderStatus = status;
        }

        public Guid Id { get; set; }
        public string Member { get; set; }
        public string Staff { get; set; }
        public string RentDate { get; set; }
        public string ReturnDate { get; set; }
        public string Warranty { get; set; }
        public string WarrantyNumber { get; set; }
        public string Total { get; set; }
        public string OrderStatus { get; set; }
    }
}
